﻿/**********************************************
 * 功能名称：产品作业路径定义
编写人：  杨莹
建立时间： 杨 2009年3月11日
说明：
加载页面时，列表所有产品ID的作业中心路径
 * 可对作业中心路径进行修改。
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.ProductInProcess
{
    public partial class PIPDiliverMapSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {



   
            
            
            }

        }

        //点击查询出查询的产品路径
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string myQueryValue = DropDownList_QueryIndex.SelectedValue;
            string myQueryText = TextBox_QueryValue.Text;
            if (myQueryValue == "0")
            {
                LoadCenterPath(myQueryText, "0");
            
            }
            else if (myQueryValue == "1") 
            {
                LoadCenterPath(myQueryText, "1");
            }
        }

        private void LoadCenterPath(string myQueryValue ,string QueryIndex)
        {
            int LastRow = 0;
            if (QueryIndex == "0")
            {
             


                var MyPathCaches = from n in db.TABC_JS_Product
                                   join m in db.TABC_JS_ProductPathCache_Map
                                   on n.产品账户ID equals m.产品ID into temps
                                   where n.产品账户ID.StartsWith(myQueryValue)
                                   orderby n.产品账户ID
                                   from temp in temps.DefaultIfEmpty()
                                   select new
                                   {
                                      产品ID = n.产品账户ID,
                                      产品账户名称 = n.产品账户名称,
                                      路径 = (temp == null ? "":temp.路径)

                                   };
                                   
                if (MyPathCaches.Count() == 0)
                {
                  
                    alert = "<script>alert('没有符合当前查询条件的记录');</script>";
                    return;
                }

                string[,] MyGridViewList = new string[MyPathCaches.Count(), 3];
                int i = 0;

                foreach (var MyPathCache in MyPathCaches)
                {

                    LastRow = 0;
                    string pathstring = MyPathCache.路径;
                    string PathRow = "";
                    if (pathstring != "" && pathstring != null)
                    {
                        string[] MyCenterPIPlists = pathstring.Split(',');

                        string MyCenterlist_lastOne = "";
                     

                        foreach (string MyCenterPIPlist in MyCenterPIPlists)
                        {

                           
                            MyCenterlist_lastOne = MyCenterPIPlist;
                            var MyCenterName = (from a in db.TABC_JS_ProductInProcess
                                                join b in db.TABC_JS_CostCenter
                                                on a.作业中心ID equals b.作业中心ID
                                                where a.中间体ID == MyCenterPIPlist
                                                select new
                                                {
                                                    a.作业中心ID,
                                                    b.作业中心名称,
                                                    a.是否中间体

                                                }).Single();
                            if (LastRow == MyCenterPIPlists.Count()-1)
                            {

                                if (MyCenterName.是否中间体 == false)
                                { 
                                    PathRow = PathRow + "→" + MyCenterName.作业中心名称; 
                                }
                                else
                                { 
                                    PathRow = PathRow + "→" + MyCenterName.作业中心名称+"<font color = red>(此步中间体未定义为最终产出)</font>"; 
                                }
                            }
                            else 
                            {

                                if (MyCenterName.是否中间体 == false)
                                {
                                    PathRow = PathRow + "→" + MyCenterName.作业中心名称 + "<font color = red>此步中间体已定义为最终产出</font>";
                                }
                                else
                                {
                                    PathRow = PathRow + "→" + MyCenterName.作业中心名称 ;
                                }
                            
                            }
                          
                          
                            LastRow = LastRow + 1;
                            
                        }

                    }
                    MyGridViewList[i, 0] = MyPathCache.产品ID;
                    MyGridViewList[i, 1] = MyPathCache.产品账户名称;
                    MyGridViewList[i, 2] = PathRow;

                    i++;
                }

                GridView_PathCache.DataSource = ConvertToDataTable(MyGridViewList);
                GridView_PathCache.DataBind();  
            
            }
            else if (QueryIndex == "1")
            {

              

                var MyPathCaches = from n in db.TABC_JS_Product
                                   join m in db.TABC_JS_ProductPathCache_Map
                                   on n.产品账户ID equals m.产品ID into temps
                                   where n.产品账户名称.Contains(myQueryValue)
                                   orderby n.产品账户ID
                                   from temp in temps.DefaultIfEmpty()
                                   select new
                                   {
                                       产品ID = n.产品账户ID,
                                       产品账户名称 = n.产品账户名称,
                                       路径 = (temp == null ? "": temp.路径)

                                   };
                if (MyPathCaches.Count() == 0)
                {

                    alert = "<script>alert('没有符合当前查询条件的记录');</script>";
                    return;
                }

                string[,] MyGridViewList = new string[MyPathCaches.Count(), 3];
                int i = 0;

                foreach (var MyPathCache in MyPathCaches)
                {

                    LastRow = 0;
                    string pathstring = MyPathCache.路径;
                    string PathRow = "";
                    if (pathstring != "" && pathstring != null)
                    {
                        string[] MyCenterPIPlists = pathstring.Split(',');

                        string MyCenterlist_lastOne = "";

                        foreach (string MyCenterPIPlist in MyCenterPIPlists)
                        {
                            MyCenterlist_lastOne = MyCenterPIPlist;
                            var MyCenterName = (from a in db.TABC_JS_ProductInProcess
                                                join b in db.TABC_JS_CostCenter
                                                on a.作业中心ID equals b.作业中心ID
                                                where a.中间体ID == MyCenterPIPlist
                                                select new
                                                {
                                                    a.作业中心ID,
                                                    b.作业中心名称,
                                                    a.是否中间体
                                                }).Single();

                            if (LastRow == MyCenterPIPlists.Count()-1)
                            {

                                if (MyCenterName.是否中间体 == false)
                                {
                                    PathRow = PathRow + "→" + MyCenterName.作业中心名称;
                                }
                                else
                                {
                                    PathRow = PathRow + "→" + MyCenterName.作业中心名称 + "<font color = red>(此步中间体未定义为最终产出)</font>";
                                }
                            }
                            else
                            {

                                if (MyCenterName.是否中间体 == false)
                                {
                                    PathRow = PathRow + "→" + MyCenterName.作业中心名称 + "<font color = red>此步中间体已定义为最终产出</font>";
                                }
                                else
                                {
                                    PathRow = PathRow + "→" + MyCenterName.作业中心名称;
                                }

                            }


                            LastRow = LastRow + 1;
                        }

                    }
                    MyGridViewList[i, 0] = MyPathCache.产品ID;
                    MyGridViewList[i, 1] = MyPathCache.产品账户名称;
                    MyGridViewList[i, 2] = PathRow;

                    i++;
                }

                GridView_PathCache.DataSource = ConvertToDataTable(MyGridViewList);
                GridView_PathCache.DataBind();  
            
            }


            
        
        }

        private DataTable ConvertToDataTable(string[,] arr)
        {

            DataTable dataSouce = new DataTable();
            //for (int i = 0; i < arr.GetLength(1); i++)
            //{
            DataColumn newColumn1 = new DataColumn("产品ID", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn1);
            DataColumn newColumn2 = new DataColumn("产品名称", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn2);
            DataColumn newColumn3 = new DataColumn("路径", arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn3);
            //}
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                DataRow newRow = dataSouce.NewRow();
                newRow[0] = arr[i, 0];
                newRow[1] = arr[i, 1];
                newRow[2] = arr[i, 2];

                dataSouce.Rows.Add(newRow);
            }
            return dataSouce;

        }



        protected void Button_PathEdit_Command(object sender, CommandEventArgs e)
        {
            string myProductID = e.CommandArgument.ToString();

            Response.Redirect("PIPDiliverMap_Edit.aspx?productid=" + myProductID);
        }

        protected void Button_Update_Click(object sender, EventArgs e)
        {
            try
            {

                db.产品路径映射MAP导入(Application["accountPeriod"].ToString());

         
                alert = "<script>alert('更新成功');</script>";
            }
            catch
            {

    
                alert = "<script>alert('更新失败');</script>";
            }
        }



        //刷新所有的产品路径设置。
        protected void Button_Refresh_Click(object sender, EventArgs e)
        {
            try
            {
                db.CommandTimeout = 600;
                db.全部产品路径缓存Map();

                alert = "<script>alert('更新成功');</script>";
            }
            catch
            {


                alert = "<script>alert('更新失败,请检查是否有循环路径');</script>";
            }
        }


    }
}
